const connect = require('connect')

function setupLogger(format = ':method :url') {
  const regexp = /:(\w+)/g
  return function (req, res, next) {
    const str = format.replace(regexp, (match, property) => {
      if (typeof req[property] === 'string') {
        return req[property]
      }
      return JSON.stringify(req[property])
    })
    console.log(str)
    next()
  }
}

function hello(req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.end('hello world')
}

connect()
  .use(setupLogger(':method :url :headers'))
  .use(hello)
  .listen(3000)
